package com.minazuki.signinserver.service;

import com.minazuki.signinserver.config.JwtProperties;
import com.minazuki.signinserver.pojo.api.UserInfo;
import com.minazuki.signinserver.pojo.dao.*;
import com.minazuki.signinserver.utils.JwtUtils;
import com.minazuki.signinserver.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.Sqls;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class PublishService {
    @Autowired
    private PublishRecordMapper mapper;
    @Autowired
    SigninRecordMapper signinRecordMapper;
    @Autowired
    CourseMapper courseMapper;
    @Autowired
    JwtProperties jwtProperties;

    public List<PublishRecord> getPublish(Integer cid, String token) {
//        UserInfo info;
//        try {
//            info = JwtUtils.getInfoFromToken(token, prop.getPublicKey());
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new ArrayList<>();
//        }

        Example example = new Example.Builder(PublishRecord.class)
                .where(Sqls.custom().andEqualTo("cid", cid))
                .build();
        List<PublishRecord> publishRecords = mapper.selectByExample(example);
        return publishRecords;
    }

    public boolean addPublish(PublishRecord record, String token) {
        UserInfo info;
        try {
            info = JwtUtils.getInfoFromToken(token, jwtProperties.getPublicKey());
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        if (isCourseExist(record.getCid(), info.getId())) return false;
        record.setPid(null);
        record.setStartTime(new Timestamp(new Date().getTime()));
        record.setEndTime(new Timestamp(record.getStartTime().getTime() + record.getTimeout()));
        return mapper.insertSelective(record) > 0;
    }

    private boolean isCourseExist(int cid, int uid) {
        Course course = new Course();
        course.setId(cid);
        course.setUid(uid);
        if (courseMapper.selectCount(course) == 0)
            return true;
        return false;
    }

    public boolean endPublish(Integer pid, String token) {
        UserInfo info;
        try {
            info = JwtUtils.getInfoFromToken(token, jwtProperties.getPublicKey());
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        PublishRecord record = new PublishRecord();
        record.setPid(pid);
        record = mapper.selectByPrimaryKey(record);
        if (record == null) return false;

        if (isCourseExist(record.getCid(), info.getId())) return false;

        record.setEndTime(new Timestamp(new Date().getTime()));
        return mapper.updateByPrimaryKeySelective(record) > 0;

    }

    public boolean delayPublish(Integer pid, int time, String token) {
        UserInfo info = JwtUtils.getInfoFromTokenForSure(token, jwtProperties.getPublicKey());
        PublishRecord record = new PublishRecord();
        record.setPid(pid);
        record = mapper.selectByPrimaryKey(record);
        if (record == null) return false;
        if (isCourseExist(record.getCid(), info.getId())) return false;

        if (record.getEndTime().after(new Date()))
            record.setEndTime(new Timestamp(record.getEndTime().getTime() + TimeUtils.minToMicrosecond(time)));
        else record.setEndTime(new Timestamp(new Date().getTime() + TimeUtils.minToMicrosecond(time)));

        return mapper.updateByPrimaryKeySelective(record) > 0;

    }


    public List<PublishRecord> getPublishForSub(int cid, String token) {
        UserInfo info;
        try {
            info = JwtUtils.getInfoFromToken(token, jwtProperties.getPublicKey());
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList();
        }

        Example example = new Example.Builder(PublishRecord.class)
                .where(Sqls.custom().andEqualTo("cid", cid))
                .build();
        List<PublishRecord> publishRecords = mapper.selectByExample(example);
        for (int i = 0; i < publishRecords.size(); i++) {
            SigninRecord signinRecord = new SigninRecord();
            signinRecord.setPid(publishRecords.get(i).getPid());
            signinRecord.setUid(info.getId());
            if (signinRecordMapper.selectCount(signinRecord) > 0) {
                publishRecords.get(i).setSigned(true);
            } else publishRecords.get(i).setSigned(false);
        }
        return publishRecords;
    }
}
